package jpg1.jpg1zad5;

/**
 * Autor: Daniel Michalski
 * Data: 05.07.13
 */

public class Main {
    public static void main(String[] args) {
        testujInterfejsRunnable();
        testujKlaseThread();
    }

    private static void testujKlaseThread() {
        MyThread[] threads1 = new MyThread[10];

        for (int i = 0; i < threads1.length; i++) {
            threads1[i] = new MyThread();
            threads1[i].start();
        }
    }

    private static void testujInterfejsRunnable() {
        Runnable[] runnableTable = new Runnable[10];
        Thread[] threads = new Thread[10];

        for (int i = 0; i < 10; i++) {
            runnableTable[i] = new MojRunnable(i);
        }

        for (int i = 0; i < 10; i++) {
            threads[i] = new Thread(runnableTable[i]);
        }

        for (int i = 0; i < 10; i++) {
            threads[i].start();
        }
    }
}

class MyThread extends Thread {
    @Override
    public void run() {
        int i = 1;
        while (true) {
            System.out.println(i++);

            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

class MojRunnable implements Runnable{
    private int id;

    public MojRunnable(int id) {
        this.id = id;
    }

    @Override
    public void run() {
        while (true) {
            System.out.println("Wątek " + id);

            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                System.out.println("InterruptedException: " + e);
            }
        }
    }
}


